﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Web.Models;
using Batman.Cms.Core.Services;
using Batman.FormGenerator.Controllers;
using Batman.FormGenerator.Domain;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Batman.FormGenerator.Services
{
    public class FormAppSvc : ServiceBase<Form>
    {
        public FormAppSvc(IRepository<Form> repository) : base(repository)
        {
        }

        public PagedResultDto<Form> GetForms(PagedResultRequestDto page)
        {
            var query = Repository.GetAll().AsNoTracking();
            var total = Repository.Count();
            var records = query.OrderByDescending(t => t.Id).Skip(page.SkipCount).Take(page.MaxResultCount).ToList();

            return new PagedResultDto<Form>(total, records);
        }

        public AjaxResponse Create(CreateFormInput model)
        {
            var entity = ObjectMapper.Map<Form>(model);

            Repository.Insert(entity);

            return new AjaxResponse();
        }
    }
}
